package no.ffi.gismoidm.attrex;

import java.util.Properties;
import java.util.StringTokenizer;

/**
 * Check is a string of komma-separated tokens contains a given token value
 * @author anders
 */
public class HasToken extends BooleanOperator {
    
    StringOperand tokenList,token;
    String separator;
    public HasToken(StringOperand tokenList, StringOperand token, String separator) {
        this.tokenList = tokenList;
        this.token = token;
        this.separator = separator;
    }
    
    public HasToken(StringOperand tokenList, StringOperand token) {
        this(tokenList,token,",");
    }
    
    public HasToken(String tokenVariable, String token) {
        this(new AttributeOperand(tokenVariable),new StringOperand(token));
    }

    @Override
    public BooleanValue getBooleanValue(Properties attributes) throws AttributeEvaluationException {
        String tkList = tokenList.getStringValue(attributes).toString();
        String tk = token.getStringValue(attributes).toString();
        StringTokenizer stok = new StringTokenizer(tkList,separator);
        while (stok.hasMoreTokens()) {
            String elem = stok.nextToken().trim();
            if (elem.equals(tk)) return new BooleanValue(true);
        }
        return new BooleanValue(false);
    }
    
    public String toString() {
        return "hastoken(" + tokenList.toString() + "," + token.toString() + ")";
    }

    
}
